<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="10px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="字典编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="字典编码"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="启用状态" prop="active">
        <el-switch
          v-model="dataForm.active"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="启用"
          inactive-text="不启用"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        code: "",
        description: "",
        active: "",
        addTime: "",
        addUser: "",
        editTime: "",
        editUser: "",
        editable: 1,
      },
      dataRule: {
        code: [
          { required: true, message: "字典编码不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" },
        ],
        active: [
          {
            required: true,
            message: "0：不启用，1：启用不能为空",
            trigger: "blur",
          },
        ],
        addTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        addUser: [{ required: true, message: "不能为空", trigger: "blur" }],
        editTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        editUser: [{ required: true, message: "不能为空", trigger: "blur" }],
        editable: [
          {
            required: true,
            message: "可编辑 1可修改 0系统数据，不可修改不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/dict/tbbasedict/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.code = data.tbBaseDict.code;
              this.dataForm.description = data.tbBaseDict.description;
              this.dataForm.active = data.tbBaseDict.active;
              this.dataForm.addTime = data.tbBaseDict.addTime;
              this.dataForm.addUser = data.tbBaseDict.addUser;
              this.dataForm.editTime = data.tbBaseDict.editTime;
              this.dataForm.editUser = data.tbBaseDict.editUser;
              this.dataForm.editable = data.tbBaseDict.editable;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/dict/tbbasedict/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              code: this.dataForm.code,
              description: this.dataForm.description,
              active: this.dataForm.active,
              addTime: this.dataForm.addTime,
              addUser: this.dataForm.addUser,
              editTime: this.dataForm.editTime,
              editUser: this.dataForm.editUser,
              editable: this.dataForm.editable,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
